<template slot-scope="scope">
    <div>
        <van-nav-bar title="面经"/>
        <van-form>
            <van-field
            label="用户名"
            placeholder="请输入用户名"
            v-model="username"
            ></van-field>
            <van-field
            label="密码"
            placeholder="请输入密码"
            type="password"
            v-model="password"
            ></van-field>
        <div style="margin:16px">
            <van-button type="info" @click="onlogin" block>提交</van-button>
        </div>
    </van-form>
    <router-link to="/register" class="link">注册账号</router-link>

    </div>
</template>
<script>
import {login} from '@/api/user'
import {setToken} from '@/utils/storage'
export default{
    data(){
        return{
            username:'',
            password:''
        }
    },
    methods:{
       async onlogin(){
        this.$toast.loading({
            message:"正在登录中....",
            forbidClick:true
            
        })

        try{
           const res= await login({username:this.username,password:this.password})
           console.log(res)
           setToken(res.data.token)
            this.$toast.success("登录成功")
            this.$router.push('/')
        }catch{
            this.$toast.fail("注册失败")
        }
            
        }
    }
}
</script>
<!-- scoped防止溢出影响其他页面 -->
<style scoped lang="less">
.link{
    float: right;
    padding-right: 20px;
    font-size:10px
}
</style>
